package com.cf.data.serve.environment;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ZhAnQuanMaoServer  extends WebSocketClient {
    static String url = "https://caps.runde.pro/api/index.php?ctl=tool&act=get_pkey";
    static String user_name = "CS940B";
    static String pwd = "CS123456dl@";
    static String j = "";
    static Map<String,Object> f = null;


    public static void main(String[] args){
        try {
            // 目标URL
            String targetUrl = url;
            // 表单数据，格式为key1=value1&key2=value2
            String formData = "user_name=CS940B&pwd=CS123456dl@";

            // 发送表单数据
            //pkey(targetUrl, formData);
/*            hqzdglyxsyzfhcylb("14719");
            pkey(url,formData);*/
            tjfz("分组1","14719");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //添加分组
    public static String tjfz(String g_name,String admin_id) throws Exception {
        String token = "";
        //获取pkey
        String formData = "user_name=CS940B&pwd=CS123456dl@";
        // 创建URL对象
        URL url = new URL("https://caps.runde.pro/api/index.php?ctl=tool&act=get_pkey");
        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        Map<String,Object> map;
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                //System.out.println(responseLine);
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);

        }

        // 关闭连接
        connection.disconnect();


        //获取token
        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "user_name=CS940B&pkey="+map.get("data");
        // 创建URL对象
        url = new URL("https://caps.runde.pro/api/index.php?ctl=tool&act=get_token");
        // 打开连接
        connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);
            j="{\"act\": \"ma_login\",\"user_name\": \"CS940B\",\"access_token\":\""+map.get("token")+"\"}";
        }
        token = String.valueOf(map.get("token"));
        // 关闭连接
        connection.disconnect();



        //添加分组
        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "admin_id="+admin_id+"&g_name="+g_name+"&token="+token;
        // 创建URL对象
        url = new URL("https://caps.runde.pro/api/index.php?ctl=group&act=create_group");
        // 打开连接
        connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        // 读取响应
        String json = "";
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;

            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
        }
        System.out.println(json);
        // 关闭连接
        connection.disconnect();
        return json;
    }

    //删除分组
    public String scfz(String g_id,String admin_id) throws Exception {
        String token = "";
        //获取pkey
        String formData = "user_name=CS940B&pwd=CS123456dl@";
        // 创建URL对象
        URL url = new URL("https://caps.runde.pro/api/index.php?ctl=tool&act=get_pkey");
        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        Map<String,Object> map;
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                //System.out.println(responseLine);
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);

        }

        // 关闭连接
        connection.disconnect();


        //获取token
        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "user_name=CS940B&pkey="+map.get("data");
        // 创建URL对象
        url = new URL("https://caps.runde.pro/api/index.php?ctl=tool&act=get_token");
        // 打开连接
        connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);
            j="{\"act\": \"ma_login\",\"user_name\": \"CS940B\",\"access_token\":\""+map.get("token")+"\"}";
        }
        token = String.valueOf(map.get("token"));
        // 关闭连接
        connection.disconnect();


        //添加分组
        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "admin_id="+admin_id+"&g_id="+g_id+"&token="+token;
        // 创建URL对象
        url = new URL("https://caps.runde.pro/api/index.php?ctl=group&act=del_group");
        // 打开连接
        connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        // 读取响应
        String json = "";
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;

            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
        }
        System.out.println(json);
        // 关闭连接
        connection.disconnect();
        return json;
    }

    //添加人员
    public String tjry(String admin_id,String g_id,String cap_type,String device_id,String mobile,String pwd,String sim,String user_name,String real_name,String user_img,String role,String department,String watch,String watch_mac,String b1,String b2,String headset,String gas_mac,String uwb_device_id) throws Exception {
        String token = "";
        //获取pkey
        String formData = "user_name=CS940B&pwd=CS123456dl@";
        // 创建URL对象
        URL url = new URL("https://caps.runde.pro/api/index.php?ctl=tool&act=get_pkey");
        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        Map<String,Object> map;
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                //System.out.println(responseLine);
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);

        }

        // 关闭连接
        connection.disconnect();


        //获取token
        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "user_name=CS940B&pkey="+map.get("data");
        // 创建URL对象
        url = new URL("https://caps.runde.pro/api/index.php?ctl=tool&act=get_token");
        // 打开连接
        connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);
            j="{\"act\": \"ma_login\",\"user_name\": \"CS940B\",\"access_token\":\""+map.get("token")+"\"}";
        }
        token = String.valueOf(map.get("token"));
        // 关闭连接
        connection.disconnect();

        //添加分组
        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "admin_id="+admin_id+"&g_id="+g_id+"&cap_type="+cap_type+"&device_id="+device_id+"&mobile="+mobile+"&pwd="+pwd+"&sim="+sim+"&user_name="+user_name+"&real_name="+real_name+"&user_img="+user_img+"&role="+role+"&department="+department+"&watch="+watch+"&watch_mac="+watch_mac+"&b1="+b1+"&b2="+b2+"&headset="+headset+"&gas_mac="+gas_mac+"&uwb_device_id="+uwb_device_id;
        // 创建URL对象
        url = new URL("https://caps.runde.pro/api/index.php?ctl=group&act=add_user");
        // 打开连接
        connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        // 读取响应
        String json = "";
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;

            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
        }

        // 关闭连接
        connection.disconnect();
        return json;
    }

    //删除人员
    public String scry(String user_id,String admin_id) throws Exception {
        String token = "";
        //获取pkey
        String formData = "user_name=CS940B&pwd=CS123456dl@";
        // 创建URL对象
        URL url = new URL("https://caps.runde.pro/api/index.php?ctl=tool&act=get_pkey");
        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        Map<String,Object> map;
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                //System.out.println(responseLine);
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);

        }

        // 关闭连接
        connection.disconnect();


        //获取token
        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "user_name=CS940B&pkey="+map.get("data");
        // 创建URL对象
        url = new URL("https://caps.runde.pro/api/index.php?ctl=tool&act=get_token");
        // 打开连接
        connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);
            j="{\"act\": \"ma_login\",\"user_name\": \"CS940B\",\"access_token\":\""+map.get("token")+"\"}";
        }
        token = String.valueOf(map.get("token"));
        // 关闭连接
        connection.disconnect();

        //添加分组
        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "admin_id="+admin_id+"&user_id="+user_id+"&token="+token;
        // 创建URL对象
        url = new URL("https://caps.runde.pro/api/index.php?ctl=group&act=del_user");
        // 打开连接
        connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        // 读取响应
        String json = "";
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;

            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
        }

        // 关闭连接
        connection.disconnect();
        return json;
    }

    //获取指定管理员下所有分组和成员列表
    public static Map<String,Object> hqzdglyxsyzfhcylb(String admin_id) throws Exception {
        String token = "";
        //获取pkey
        String formData = "user_name=CS940B&pwd=CS123456dl@";
        // 创建URL对象
        URL url = new URL("https://caps.runde.pro/api/index.php?ctl=tool&act=get_pkey");
        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        Map<String,Object> map;
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                //System.out.println(responseLine);
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);

        }

        // 关闭连接
        connection.disconnect();


        //获取token
        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "user_name=CS940B&pkey="+map.get("data");
        // 创建URL对象
        url = new URL("https://caps.runde.pro/api/index.php?ctl=tool&act=get_token");
        // 打开连接
        connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);
            j="{\"act\": \"ma_login\",\"user_name\": \"CS940B\",\"access_token\":\""+map.get("token")+"\"}";
        }
        token = String.valueOf(map.get("token"));
        // 关闭连接
        connection.disconnect();

        //添加分组
        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "admin_id="+admin_id+"&token="+token;
        // 创建URL对象
        url = new URL("https://caps.runde.pro/api/index.php?ctl=bruce&act=get_my_group_members");
        // 打开连接
        connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        // 读取响应
        String json = "";
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;

            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
        }
        JSONObject jsonObject = JSON.parseObject(json);
        map = jsonObject.getInnerMap();

        // 关闭连接
        connection.disconnect();
        System.out.println(map);
        return map;
    }

    //获取指定时间段内轨迹记录
    public static String hqzdsjdngjjl(String admin_id,String user_id,String start,String end) throws Exception {
        String token = "";
        //获取pkey
        String formData = "user_name=CS940B&pwd=CS123456dl@";
        // 创建URL对象
        URL url = new URL("https://caps.runde.pro/api/index.php?ctl=tool&act=get_pkey");
        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        Map<String,Object> map;
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                //System.out.println(responseLine);
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);

        }

        // 关闭连接
        connection.disconnect();


        //获取token
        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "user_name=CS940B&pkey="+map.get("data");
        // 创建URL对象
        url = new URL("https://caps.runde.pro/api/index.php?ctl=tool&act=get_token");
        // 打开连接
        connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);
            j="{\"act\": \"ma_login\",\"user_name\": \"CS940B\",\"access_token\":\""+map.get("token")+"\"}";
        }
        token = String.valueOf(map.get("token"));
        // 关闭连接
        connection.disconnect();



        //添加分组
        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "admin_id="+admin_id+"&user_id="+user_id+"&start="+start+"&end="+end+"&token="+token;
        // 创建URL对象
        //有过滤处理
        //url = new URL("https://caps.runde.pro/api/index.php?ctl=location&act=get_user_path_web");
        //无过滤处理
        url = new URL("https://caps.runde.pro/api/index.php?ctl=location&act=get_user_path_1");
        // 打开连接
        connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        // 读取响应
        String json = "";
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;

            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
        }
        System.out.println(json);
        // 关闭连接
        connection.disconnect();
        return json;
    }

    //获取分组
    public static Map<String,Object> hqfz(String admin_id) throws Exception {
        String token = "";
        //获取pkey
        String formData = "user_name=CS940B&pwd=CS123456dl@";
        // 创建URL对象
        URL url = new URL("https://caps.runde.pro/api/index.php?ctl=tool&act=get_pkey");
        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        Map<String,Object> map;
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                //System.out.println(responseLine);
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);

        }

        // 关闭连接
        connection.disconnect();


        //获取token
        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "user_name=CS940B&pkey="+map.get("data");
        // 创建URL对象
        url = new URL("https://caps.runde.pro/api/index.php?ctl=tool&act=get_token");
        // 打开连接
        connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);
            j="{\"act\": \"ma_login\",\"user_name\": \"CS940B\",\"access_token\":\""+map.get("token")+"\"}";
        }
        token = String.valueOf(map.get("token"));
        // 关闭连接
        connection.disconnect();

        //添加分组
        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "admin_id="+admin_id+"&token="+token;
        // 创建URL对象
        url = new URL("https://caps.runde.pro/api/index.php?ctl=group&act=get_group_list");
        // 打开连接
        connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        // 读取响应
        String json = "";
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;

            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
        }
        JSONObject jsonObject = JSON.parseObject(json);
        map = jsonObject.getInnerMap();

        // 关闭连接
        connection.disconnect();
        System.out.println(map);
        return map;
    }

    //获取电子围栏列表
    public static Map<String,Object> hqdzwllb(String admin_id) throws Exception {
        String token = "";
        //获取pkey
        String formData = "user_name=CS940B&pwd=CS123456dl@";
        // 创建URL对象
        URL url = new URL("https://caps.runde.pro/api/index.php?ctl=tool&act=get_pkey");
        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        Map<String,Object> map;
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                //System.out.println(responseLine);
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);

        }

        // 关闭连接
        connection.disconnect();


        //获取token
        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "user_name=CS940B&pkey="+map.get("data");
        // 创建URL对象
        url = new URL("https://caps.runde.pro/api/index.php?ctl=tool&act=get_token");
        // 打开连接
        connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);
            j="{\"act\": \"ma_login\",\"user_name\": \"CS940B\",\"access_token\":\""+map.get("token")+"\"}";
        }
        token = String.valueOf(map.get("token"));
        // 关闭连接
        connection.disconnect();

        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "admin_id="+admin_id;
        // 创建URL对象
        url = new URL("https://caps.runde.pro/api/index.php?ctl=fence&act=get_fence_list");
        // 打开连接
        connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("GET");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        // 读取响应
        String json = "";
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;

            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
        }
        JSONObject jsonObject = JSON.parseObject(json);
        map = jsonObject.getInnerMap();

        // 关闭连接
        connection.disconnect();
        System.out.println(map);
        return map;
    }





    //获取pkey
    public static void pkey(String targetUrl, String formData) throws Exception {
        // 创建URL对象
        URL url = new URL(targetUrl);
        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        Map<String,Object> map;
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                //System.out.println(responseLine);
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);

        }

        // 关闭连接
        connection.disconnect();
        // 目标URL
        // 表单数据，格式为key1=value1&key2=value2
        formData = "user_name=CS940B&pkey="+map.get("data");
        token("https://caps.runde.pro/api/index.php?ctl=tool&act=get_token", formData);
    }

    //获取pkey
    public static void token(String targetUrl, String formData) throws Exception {
        // 创建URL对象
        URL url = new URL(targetUrl);
        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为POST
        connection.setRequestMethod("POST");
        // 设置请求属性，表示是表单数据
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // 设置允许输入输出
        connection.setDoOutput(true);

        // 发送请求参数
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = formData.getBytes("UTF-8");
            os.write(input, 0, input.length);
        }

        // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
        Map<String,Object> map;
        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
            String responseLine = null;
            String json = "";
            while ((responseLine = br.readLine()) != null) {
                json = responseLine;
            }
            JSONObject jsonObject = JSON.parseObject(json);
            map = jsonObject.getInnerMap();
            System.out.println(map);
            j="{\"act\": \"ma_login\",\"user_name\": \"CS940B\",\"access_token\":\""+map.get("token")+"\"}";
        }

        // 关闭连接
        connection.disconnect();
/*        JSONObject json = new JSONObject();
        json.put("name", "John");
        json.put("age", 30);
        json.put("isStudent", false);*/
        try {
            ZhAnQuanMaoServer client = new ZhAnQuanMaoServer(new URI("wss://caps.runde.pro/wss"));
            client.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public ZhAnQuanMaoServer(URI serverUri) {
        super(serverUri);
    }

    @Override
    public void onOpen(ServerHandshake handshakedata) {
        System.out.println("新连接已打开");
        send(j);
    }

    @Override
    public void onMessage(String message) {
        Map<String,Object> map;
        System.out.println("接收到消息: " + message);
        JSONObject jsonObject = JSON.parseObject(message);
        f = jsonObject.getInnerMap();
        f = JSON.parseObject(String.valueOf(f.get("admin_info")));
    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        System.out.println("连接已关闭");
    }

    @Override
    public void onError(Exception ex) {
        ex.printStackTrace();
    }
}
